home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 November / Chip Kasım 2001.iso / prog / cdcode / vmware / setup.exe / data1.cab / WGS_PerlInit / init.pl
Encoding:
Perl Script  |  2001-09-19  |  23.5 KB  |  916 lines

  1. #!/usr/bin/perl -w
  2.  
  3. #############################################################
  4. # Copyright (C) 1998-2000 VMware, Inc.
  5. # All Rights Reserved
  6. # $Id: init.pl,v 1.46 2001/07/30 19:17:52 mgu Exp $
  7. #############################################################
  8.  
  9. #
  10. # VMServerd configuration script.  This script is used to setup the
  11. # VMserverd server.
  12. #
  13. # This script should be installed in a location that can be found
  14. # by vmserverd.  The location by default is /home/vmware/etc.
  15. #
  16.  
  17.  
  18. #############################################################
  19. # Set up include paths 
  20. #############################################################
  21. BEGIN {
  22.   $ENV{vmware_VMSERVERD} = 1;
  23.   $ENV{vmware_PROFILE} = 0;
  24.  
  25.   if ( $^O eq "MSWin32" ) {
  26.     if (-d '%winlibdir%') {
  27.        $PREFIX_PATH = '%winlibdir%';
  28.     } else {
  29.        $PREFIX_PATH = 'm:/bora-winroot/vmserverdPerl';
  30.     }
  31.     @INC = ("$PREFIX_PATH/5.00503/lib",
  32.         "$PREFIX_PATH/5.00503/lib/MSWin32-x86",
  33.         "$PREFIX_PATH/site/5.00503/lib",
  34.         "$PREFIX_PATH/site/5.00503/lib/MSWin32-x86",
  35.         "$PREFIX_PATH/vmware/perlroot",
  36.         '.'
  37.         );
  38.   } else {
  39.     if( -d '%libdir%' ) {
  40.     $PREFIX_PATH = '%libdir%';
  41.     } else {
  42.     $PREFIX_PATH = '/usr/lib/vmware';
  43.     }
  44.     @INC = ("$PREFIX_PATH/perl5",
  45.         "$PREFIX_PATH/perl5/5.00503",
  46.         "$PREFIX_PATH/perl5/5.00503/i386-linux",
  47.         "$PREFIX_PATH/perl5/site-perl/i386-linux",
  48.         "$PREFIX_PATH/perl5/site_perl/5.005",
  49.         "$PREFIX_PATH/perl5/site_perl/5.005/i386-linux",
  50.         '.'
  51.         );
  52.   }
  53. }
  54.  
  55. #############################################################
  56. # Common modules shared between GSX and ESX.
  57. #############################################################
  58. use strict;
  59. use VMware::VMServerd qw(&Log);
  60. use VMware::DOMAccess;
  61. use VMware::VMServerd::EventLog;
  62. use VMware::VMServerd::Who;
  63. use VMware::VMServerd::Info;
  64. use VMware::VMServerd::Mac;
  65. use VMware::VMServerd::Query;
  66. use VMware::VMServerd::Stats;
  67. use VMware::VMServerd::HTTP;
  68. use VMware::VMServerd::VMList;
  69.  
  70. BEGIN {
  71.     no strict;
  72.     $IS_GSX = '@@IS_WGS_SERVER@@';
  73.     $IS_ESX = '@@IS_SCALABLE_SERVER@@';
  74.  
  75.  
  76.     if( $IS_ESX ) {
  77.     # Put modules that belong to ESX here
  78.     require VMware::VMServerd::ESXHostInfo;
  79.     require VMware::VMServerd::ESXHostEdit;
  80.     require VMware::VMServerd::FileSystem;
  81.     require VMware::VMServerd::ESXResourceInfo;
  82.     require VMware::VMServerd::ESXResourceEdit;
  83.     require VMware::VMServerd::ESXVMConfigInfo;
  84.     require VMware::VMServerd::ESXVMConfigEdit;
  85.     require VMware::VMServerd::ESXVMDeleteInfo;
  86.     require VMware::VMServerd::ESXVMDeleteEdit;
  87.     }
  88.  
  89.     if( $IS_GSX ) {
  90.     # Put modules that belong to GSX here
  91.         require VMware::VMServerd::Disk;
  92.     require VMware::VMServerd::VMDeleteInfo;
  93.     require VMware::VMServerd::VMDeleteEdit;
  94.     require VMware::VMServerd::GSXHostInfo;
  95.     }
  96. }
  97.  
  98. #############################################################
  99. # Turn on verbose error/warning reporting by uncommenting
  100. # the following line:
  101. #############################################################
  102. #&VMware::VMServerd::setVerbose(1);
  103.  
  104. #############################################################
  105. # Define operations and the Perl functions to be executed
  106. # when a request for such an operation is received.
  107. # Permissions information is also indicated here.
  108. #############################################################
  109. VMware::VMServerd::addOperation( OPNAME    => 'inc',
  110.                  PERLFUNC  => 'main::increment' );
  111.  
  112. VMware::VMServerd::addOperation( OPNAME    => 'dec',
  113.                  PERLFUNC  => 'main::decrement' );
  114.  
  115. VMware::VMServerd::addOperation( OPNAME    => 'div',
  116.                  PERLFUNC  => 'main::divide' );
  117.  
  118. VMware::VMServerd::addOperation( OPNAME    => 'Directory_List',
  119.                  PERLFUNC  => 'VMware::VMServerd::Handlers::Directory_List_Handler',
  120.                  POLICY => 'authuser' );
  121.  
  122. VMware::VMServerd::addOperation( OPNAME    => 'Directory_Make',
  123.                  PERLFUNC  => 'VMware::VMServerd::Handlers::Directory_Make_Handler',
  124.                  POLICY => 'authuser' );
  125.  
  126. VMware::VMServerd::addOperation( OPNAME    => 'Directory_Remove',
  127.                  PERLFUNC  => 'VMware::VMServerd::Handlers::Directory_Remove_Handler',
  128.                  POLICY => 'authuser' );
  129.  
  130. VMware::VMServerd::addOperation( OPNAME    => 'Directory_GetSpaceUsage',
  131.                  PERLFUNC  => 'VMware::VMServerd::Handlers::Directory_GetSpaceUsage_Handler',
  132.                  POLICY => 'authuser' );
  133.  
  134. VMware::VMServerd::addOperation( OPNAME => 'File_Create',
  135.                  PERLFUNC => 'VMware::VMServerd::Handlers::File_Create_Handler',
  136.                  POLICY => 'authuser' );
  137.  
  138. VMware::VMServerd::addOperation( OPNAME => 'File_Read',
  139.                  PERLFUNC => 'VMware::VMServerd::Handlers::File_Read_Handler',
  140.                  POLICY => 'authuser' );
  141.  
  142. VMware::VMServerd::addOperation( OPNAME => 'File_Exists',
  143.                  PERLFUNC => 'VMware::VMServerd::Handlers::File_Exists_Handler',
  144.                  POLICY => 'authuser' );
  145.  
  146. VMware::VMServerd::addOperation( OPNAME => 'File_Copy',
  147.                  PERLFUNC => 'VMware::VMServerd::Handlers::File_Copy_Handler',
  148.                  POLICY => 'authuser' );
  149.  
  150. VMware::VMServerd::addOperation( OPNAME => 'File_Delete',
  151.                  PERLFUNC => 'VMware::VMServerd::Handlers::File_Delete_Handler',
  152.                  POLICY => 'authuser' );
  153.  
  154. VMware::VMServerd::addOperation( OPNAME => 'Host_GetVirtualNet',
  155.                  PERLFUNC => 'VMware::VMServerd::Handlers::Host_GetVirtualNet_Handler',
  156.                  POLICY => 'authuser' );
  157.  
  158. VMware::VMServerd::addOperation( OPNAME => 'Host_GetBridgedNet',
  159.                  PERLFUNC => 'VMware::VMServerd::Handlers::Host_GetBridgedNet_Handler',
  160.                  POLICY => 'authuser' );
  161.  
  162. VMware::VMServerd::addOperation( OPNAME => 'Host_GetHostOnlyNet',
  163.                  PERLFUNC => 'VMware::VMServerd::Handlers::Host_GetHostOnlyNet_Handler',
  164.                  POLICY => 'authuser' );
  165.  
  166. Log("\nFiles included by init.pl:\n");
  167. my $f;
  168. foreach $f (keys(%INC)) {
  169.     my $abspath = $INC{$f};
  170.     Log("   module $f -> $abspath\n");
  171. }
  172. Log("\n\n" .
  173.     "Operations defined in function table:\n" .
  174.     &VMware::VMServerd::dumpOperation() .
  175.     "\n"
  176.     );
  177.  
  178. #############################################################
  179. # Some sample Perl request handling functions.  These are 
  180. # sample functions and should really go in a separate file
  181. #############################################################
  182. sub increment($$) {
  183.     my $in = shift;
  184.     my $out = shift;
  185.  
  186.     my $s;
  187.     $s = $in->getValue(".number");
  188.     $s++;
  189.  
  190.     $out->setValue(".number", $s);
  191.     
  192.     return(1);
  193. }    
  194.  
  195. sub decrement($$) {
  196.     my $in = shift;
  197.     my $out = shift;
  198.  
  199.     my $s;
  200.     $s = $in->getValue(".number");
  201.     $s--;
  202.  
  203.     $out->setValue(".number", $s);
  204.     
  205.     return(1);
  206. }    
  207.  
  208. sub divide($$) {
  209.     my $in = shift;
  210.     my $out = shift;
  211.     
  212.     my($s, $t);
  213.     $s = $in->getValue(".number1");
  214.     $t = $in->getValue(".number2");
  215.  
  216.     if( $t == 0 ) {
  217.     die "Divide by zero";
  218.     }
  219.     
  220.     $out->setValue(".number", sprintf("%d", $s/$t));
  221.  
  222.     return(1);
  223. }    
  224.  
  225. #############################################################
  226. # Some Perl request handling functions that are common to both
  227. # SS and WGS.  These should be moved elsewhere in the future.
  228. #############################################################
  229. package VMware::VMServerd::Handlers;
  230.  
  231. my %gHelpers = (
  232.     ls => "ls",
  233.     mkdir => "mkdir",
  234.     ifconfig => "ifconfig",
  235.     df => "df"
  236. );
  237.  
  238. my $DEFAULT_DIR_MODE = 0755;
  239. my $DEFAULT_FILE_MODE = 0644;
  240.  
  241. sub isAbsolutePath($) {
  242.     my ($file) = @_;
  243.  
  244.     if (($^O =~ /MSWin32/i) &&
  245.          (($file =~ /^[a-zA-Z]:/) || ($file =~ /^\\/))) {
  246.         return 1;
  247.     }
  248.     if (($^O =~ /linux/i) &&
  249.           (substr($file, 0, 1) eq '/')) {
  250.         return 1;
  251.     }
  252.  
  253.     return 0;
  254. }
  255.  
  256. #
  257. # Gets a list of files from a directory.  
  258. #
  259. # Input format:
  260. #   directory := the absolute path name of the directory 
  261. # Output includes information about each file.  
  262. # Output format:
  263. #
  264. #   file.name := the path name of the file relative to the directory
  265. #   file.type := regular | directory | symlink | FIFO | socket |
  266. #                block | char | tty | unknown
  267. #   file.mode := an integer corresponding to the mode of the file
  268. #   file.uid := integer corresponding to owner of file
  269. #   file.gid := integer corresponding to group of file
  270. #   file.size := size of file in bytes
  271. #   file.atime := last access time since the epoch in milliseconds
  272. #   file.mtime := last modified time since the epoch in milliseconds
  273. #
  274. # Note that symlinks are simply recognized as the files to which
  275. # they point.  If the symlink is broken, it is ignored.
  276. #
  277. sub Directory_List_Handler($$) {
  278.     my $in = shift;
  279.     my $out = shift; 
  280.     my $dir = $in->getValue(".directory");
  281.  
  282.     &VMware::VMServerd::errorReset();
  283.     # Validation on the input parameters
  284.     if( !defined($dir) || $dir eq "" ) {
  285.     &VMware::VMServerd::errorPost("No directory specified");
  286.     return 0;
  287.     }
  288.  
  289.     if (! isAbsolutePath($dir)) {
  290.     &VMware::VMServerd::errorPost("Not an absolute path $dir");
  291.     return 0;
  292.     }
  293.  
  294.     if( ! -d $dir ) {
  295.     &VMware::VMServerd::errorPost("$dir is not a directory");
  296.     return 0;
  297.     }
  298.  
  299.     # Get the list of files
  300.     local *DIR;
  301.     if( !opendir(DIR, $dir) ) {
  302.     &VMware::VMServerd::errorPost("Cannot open the directory.  Reason: $!");
  303.     return 0;
  304.     }
  305.  
  306.     my @files = readdir(DIR);
  307.     closedir DIR;
  308.  
  309.     my $i;
  310.     for( $i = 0; $i <= $#files; $i++ ) {
  311.         my $abspath = &VMware::ExtHelpers::absolute_path($files[$i], $dir);
  312.  
  313.     my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
  314.         $atime, $mtime, $ctime, $blksize, $blocks);
  315.  
  316.     # Skip if we can't get file info.
  317.     if( !(($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
  318.            $atime, $mtime, $ctime, $blksize, $blocks) = stat($abspath)) ) {
  319.         next;
  320.     }
  321.     
  322.     my $filetype;
  323.     if( -f $abspath ) {
  324.         $filetype = "regular";
  325.     } elsif( -d $abspath ) {
  326.         $filetype = "directory";
  327.     } elsif( -p $abspath ) {
  328.         $filetype = "FIFO";
  329.     } elsif( -S $abspath ) {
  330.         $filetype = "socket";
  331.     } elsif( -b $abspath ) {
  332.         $filetype = "block";
  333.     } elsif( -c $abspath ) {
  334.         $filetype = "char";
  335.     } elsif( -t $abspath ) {
  336.         $filetype = "tty";
  337.     } else {
  338.         $filetype = "";
  339.     }
  340.  
  341.     $out->setValue(".file[$i].name", $files[$i]);
  342.     $out->setValue(".file[$i].type", $filetype);
  343.     $out->setValue(".file[$i].mode", $mode);
  344.     $out->setValue(".file[$i].uid", $uid);
  345.     $out->setValue(".file[$i].gid", $gid);
  346.     $out->setValue(".file[$i].size", $size);
  347.     $out->setValue(".file[$i].atime", $atime);
  348.     $out->setValue(".file[$i].mtime", $mtime);
  349.     }
  350.  
  351.     if (&VMware::VMServerd::haveError()) {
  352.       &VMware::VMServerd::errorPost("");
  353.     }
  354.    
  355.     return(1);
  356. }
  357.  
  358.  
  359. #
  360. # Makes a new directory
  361. #
  362. # Input format:
  363. #   directory := the absolute path name of the directory 
  364. #   mode := the file mode to use.  Uses $DEFAULT_DIR_MODE as default.
  365. # Output includes information about each file.  
  366. # Output format:
  367. #
  368. #   No output on success.
  369. #
  370. sub Directory_Make_Handler($$) {
  371.     my $in = shift;
  372.     my $out = shift; 
  373.     my $dir = $in->getValue(".directory");
  374.     my $mode;
  375.  
  376.     &VMware::VMServerd::errorReset();
  377.  
  378.     if ($in->hasElement(".mode") ) {
  379.     $mode = $in->getValue(".mode");
  380.     if( $mode =~ /^[0-7]+$/ ) {
  381.         $mode = oct($mode);
  382.     } elsif( $mode ne "" ) {
  383.         &VMware::VMServerd::errorPost("Invalid mode specified");
  384.         return 0;
  385.     }
  386.     }
  387.  
  388.     # Validation on the input parameters
  389.     if( !defined($dir) || $dir eq "" ) {
  390.     &VMware::VMServerd::errorPost("No directory specified");
  391.     return 0;
  392.     }
  393.  
  394.     if (! isAbsolutePath($dir)) {
  395.     &VMware::VMServerd::errorPost("Not an absolute path $dir");
  396.     return 0;
  397.     }
  398.  
  399.     if( !defined($mode) || ($mode eq "") ) {
  400.     $mode = $DEFAULT_DIR_MODE;
  401.     }
  402.  
  403.     $dir = &VMware::ExtHelpers::dir_remove_trailing_slashes($dir);
  404.  
  405.     $dir =~ s/^([a-zA-Z]:)//;
  406.     my $driveLetters = defined($1) ? $1 : '';
  407.     my @dirs = ($^O eq "MSWin32") ? split(/\\/, $dir) : split(/\//, $dir);
  408.     my $cdir = $driveLetters;
  409.     my $d;
  410.     foreach $d (@dirs) {
  411.     if( $d eq "" ) {
  412.         next;
  413.     }
  414.     $cdir .= (($^O eq "MSWin32") ? '\\' : '/') . $d;
  415.     if( -d $cdir && $cdir ne $dir ) {
  416.         next;
  417.     } else {
  418.         if( !mkdir($cdir, $mode) ) {
  419.             &VMware::VMServerd::errorPost("mkdir failed. Reason: $! ");
  420.         return 0;
  421.           }
  422.     }
  423.     }
  424.  
  425.     return(1);
  426. }
  427.  
  428.  
  429. #
  430. # Remove a directory
  431. #
  432. # Input format:
  433. #   directory := the absolute path name of the directory 
  434. # Output includes information about each file.  
  435. # Output format:
  436. #
  437. #   No output on success.
  438. #
  439. sub Directory_Remove_Handler($$) {
  440.     my $in = shift;
  441.     my $out = shift; 
  442.     my $dir = $in->getValue(".directory");
  443.  
  444.     &VMware::VMServerd::errorReset();
  445.     # Validation on the input parameters
  446.     if( !defined($dir) || $dir eq "" ) {
  447.     &VMware::VMServerd::errorPost("No directory specified");
  448.     return 0;
  449.     }
  450.  
  451.     if (! isAbsolutePath($dir)) {
  452.     &VMware::VMServerd::errorPost("Not an absolute path $dir");
  453.     return 0;
  454.     }
  455.  
  456.     if( !rmdir($dir) ) {
  457.     &VMware::VMServerd::errorPost("rmdir failed. Reason: $! ");
  458.     return 0;
  459.     }
  460.  
  461.     return(1);
  462. }
  463.  
  464.  
  465. #
  466. # Gets the space information about a directory including the
  467. # amount of space used, free, and total.
  468. #
  469. # Input format:
  470. #   directory := the absolute path name of the directory to 
  471. #                check for free space
  472. # Output includes information about each file.  
  473. # Output format:
  474. #
  475. #   used := number of kbytes that are used
  476. #   free := number of kbytes that are free
  477. #   total := total number of kbytes
  478. #
  479. sub Directory_GetSpaceUsage_Handler($$) {
  480.     my $in = shift;
  481.     my $out = shift; 
  482.     my $dir = $in->getValue(".directory");
  483.  
  484.     &VMware::VMServerd::errorReset();
  485.     # Validation on the input parameters
  486.     if( !defined($dir) || $dir eq "" ) {
  487.     &VMware::VMServerd::errorPost("No directory specified");
  488.     return 0;
  489.     }
  490.  
  491.     if (! isAbsolutePath($dir)) {
  492.     &VMware::VMServerd::errorPost("Not an absolute path $dir");
  493.     return 0;
  494.     }
  495.  
  496.     if( ! -d $dir ) {
  497.     &VMware::VMServerd::errorPost("$dir is not a directory");
  498.     return 0;
  499.     }
  500.  
  501.     local *FD;
  502.     # Use -k to get kbytes
  503.     if( !open(FD, shell_string($gHelpers{df}) . " -k " . shell_string($dir) . " |") ) {
  504.     &VMware::VMServerd::errorPost("Could not execute " . $gHelpers{df});
  505.     return 0;
  506.     }
  507.  
  508.     my $total;
  509.     my $used;
  510.     my $free;
  511.  
  512.     while(<FD>) {
  513.     if( /\/dev\/\S+\s+(\d+)\s+(\d+)\s+(\d+)/ ) {
  514.         $total = $1;
  515.         $used = $2;
  516.         $free = $3;
  517.     }
  518.     }
  519.     close(FD);
  520.  
  521.     if( !defined($total) || !defined($used) || !defined($free) ) {
  522.     &VMware::VMServerd::errorPost("Could not read directory space usage information");
  523.     return 0;
  524.     }
  525.  
  526.     $out->setValue(".total", $total);
  527.     $out->setAttribute(".total", "units", "kbyte");
  528.     $out->setValue(".used", $used);
  529.     $out->setAttribute(".used", "units", "kbyte");
  530.     $out->setValue(".free", $free);
  531.     $out->setAttribute(".free", "units", "kbyte");
  532.  
  533.     return(1);
  534. }
  535.  
  536. #
  537. # Create a file with the given contents
  538. #
  539. # Input format:
  540. #   file := the absolute path name of the file to be created
  541. #   data := the data to put into the new file
  542. #   mode := the mode of the file (optional)
  543. #   o_create := flag to ignore the check for existence of the file.
  544. #               passing 1 to this parameter will cause 
  545. # Output includes information about each file.  
  546. # Output format:
  547. #
  548. #   No output on success.
  549. #
  550. sub File_Create_Handler($$) {
  551.     my $in = shift;
  552.     my $out = shift; 
  553.     my $file = $in->getValue(".file");
  554.     my $data = $in->getValue(".data");
  555.     my $o_create = $in->getValue(".o_create");
  556.     my $mode;
  557.     my $modeStr;
  558.  
  559.     &VMware::VMServerd::errorReset();
  560.  
  561.     if ($in->hasElement(".mode")) {
  562.     $mode = $in->getValue(".mode");
  563.     $modeStr = $mode;
  564.     if( $mode =~ /^[0-7]+$/ ) {
  565.         $mode = oct($mode);
  566.     } elsif( $mode ne "" ) {
  567.         &VMware::VMServerd::errorPost("Invalid mode specified");
  568.         return 0;
  569.     }
  570.     }
  571.  
  572.     # Validation on the input parameters
  573.     if( !defined($file) || $file eq "" ) {
  574.     &VMware::VMServerd::errorPost("No file specified");
  575.     return 0;
  576.     }
  577.  
  578.     if (! isAbsolutePath($file)) {
  579.     &VMware::VMServerd::errorPost("Not an absolute path $file");
  580.     return 0;
  581.     }
  582.  
  583.     if( !defined($mode) || ($mode eq "") ) {
  584.     $mode = $DEFAULT_FILE_MODE;
  585.     }
  586.  
  587.     if( $o_create ne "1" && -e $file ) {
  588.     &VMware::VMServerd::errorPost("File exists");
  589.     return 0;
  590.     }
  591.  
  592.     local *FD;
  593.     if( !open(FD, "> $file") ) {
  594.     &VMware::VMServerd::errorPost("File open failed. Reason: $!");
  595.     return 0;
  596.     }
  597.  
  598.     print FD $data;
  599.     close(FD);
  600.  
  601.     if( !chmod($mode, $file) ) {
  602.     &VMware::VMServerd::errorPost("File created but could not change mode to " . $modeStr);
  603.     return 0;
  604.     }
  605.  
  606.     return(1);
  607. }
  608.  
  609.  
  610. #
  611. # Get the contents of a file.
  612. #
  613. # Input format:
  614. #   file := the absolute path name of the file to be read.
  615. # Output includes information about each file.  
  616. # Output format:
  617. #
  618. #   data := the data in the file
  619. #
  620. sub File_Read_Handler($$) {
  621.     my $in = shift;
  622.     my $out = shift; 
  623.     my $file = $in->getValue(".file");
  624.     my $data = "";
  625.  
  626.     &VMware::VMServerd::errorReset();
  627.     # Validation on the input parameters
  628.     if( !defined($file) || $file eq "" ) {
  629.     &VMware::VMServerd::errorPost("No file specified");
  630.     return 0;
  631.     }
  632.  
  633.     if (! isAbsolutePath($file)) {
  634.     &VMware::VMServerd::errorPost("Not an absolute path $file");
  635.     return 0;
  636.     }
  637.  
  638.     local *FD;
  639.     if( !open(FD, "< $file") ) {
  640.     &VMware::VMServerd::errorPost("Open failed. Reason: $!");
  641.     return 0;
  642.     }
  643.  
  644.     while(<FD>) {
  645.     $data .= $_;
  646.     }
  647.     close(FD);
  648.  
  649.     $out->setValue(".data", $data);
  650.  
  651.     return(1);
  652. }
  653.  
  654.  
  655. #
  656. # Checks if a file/directory exists.  Gets the permissions on the file.
  657. #
  658. # Input format:
  659. #   file := the absolute path name of the file to be read.
  660. # Output includes information about each file.  
  661. # Output format:
  662. #   exists := 0 | 1
  663. #   directory := 0 | 1
  664. #   read := 0 | 1
  665. #   write := 0 | 1
  666. #   execute := 0 | 1
  667. #
  668. sub File_Exists_Handler($$) {
  669.     my $in = shift;
  670.     my $out = shift; 
  671.     my $file = $in->getValue(".file");
  672.  
  673.     &VMware::VMServerd::errorReset();
  674.     $out->setValue(".exists", 0);
  675.     $out->setValue(".directory", 0);
  676.     $out->setValue(".read", 0);
  677.     $out->setValue(".write", 0);
  678.     $out->setValue(".execute", 0);
  679.  
  680.     # Validation on the input parameters
  681.     if( !defined($file) || $file eq "" ) {
  682.       &VMware::VMServerd::errorPost("No file specified");
  683.       return 0;
  684.     }
  685.  
  686.     if (! isAbsolutePath($file)) {
  687.     &VMware::VMServerd::errorPost("Not an absolute path $file");
  688.     return 0;
  689.     }
  690.  
  691.     my $exists = (-e $file ? 1 : 0);
  692.     my $directory = (-d $file ? 1 : 0);
  693.     my $read = (-r $file ? 1 : 0);
  694.     my $write = (-w $file ? 1 : 0);
  695.     my $execute = (-x $file ? 1 : 0);
  696.  
  697.     $out->setValue(".exists", $exists);
  698.     $out->setValue(".directory", $directory);
  699.     $out->setValue(".read", $read);
  700.     $out->setValue(".write", $write);
  701.     $out->setValue(".execute", $execute);
  702.  
  703.     return(1);
  704. }
  705.  
  706.  
  707. #
  708. # Copy a file.
  709. #
  710. # Input format:
  711. #   src := the absolute path name of the file to be read.
  712. #   dest := the absoluate path name of the file to be written.
  713. # Output includes information about each file.  
  714. # Output format:
  715. #
  716. #   No output on success.
  717. #
  718. sub File_Copy_Handler($$) {
  719.     my $in = shift;
  720.     my $out = shift; 
  721.     my $src = $in->getValue(".src");
  722.     my $dest = $in->getValue(".dest");
  723.     my $data = "";
  724.  
  725.     &VMware::VMServerd::errorReset();
  726.     # Validation on the input parameters
  727.     if( !defined($src) || $src eq "" ) {
  728.     &VMware::VMServerd::errorPost("No input file specified");
  729.     return 0;
  730.     }
  731.  
  732.     # Validation on the input parameters
  733.     if( !defined($dest) || $dest eq "" ) {
  734.         &VMware::VMServerd::errorPost("No output file specified");
  735.     return 0;
  736.     }
  737.  
  738.     if (! isAbsolutePath($src)) {
  739.     &VMware::VMServerd::errorPost("Not an absolute path: $src");
  740.     return 0;
  741.     }
  742.  
  743.     if (! isAbsolutePath($dest)) {
  744.     &VMware::VMServerd::errorPost("Not an absolute path: $dest");
  745.     return 0;
  746.     }
  747.  
  748.     local(*RFD, *WFD);
  749.     if( !open(RFD, "< $src") ) {
  750.     &VMware::VMServerd::errorPost("Open for $src failed. Reason: $!");
  751.     return 0;
  752.     }
  753.  
  754.     if( !open(WFD, "> $dest") ) {
  755.     &VMware::VMServerd::errorPost("Open for $dest failed. Reason: $!");
  756.     close(RFD);
  757.     return 0;
  758.     }
  759.  
  760.     # Some crazy OSs distinguish between text and binary files.
  761.     binmode(RFD);
  762.     binmode(WFD);
  763.     
  764.     while(<RFD>) {
  765.     print WFD;
  766.     }
  767.  
  768.     close(RFD);
  769.     close(WFD);
  770.  
  771.     return(1);
  772. }
  773.  
  774.  
  775. #
  776. # Deletes a file.
  777. #
  778. # Input format:
  779. #   file := the absolute path name of the file to be deleted
  780. # Output includes information about each file.  
  781. # Output format:
  782. #
  783. #   No output on success.
  784. #
  785. sub File_Delete_Handler($$) {
  786.     my $in = shift;
  787.     my $out = shift; 
  788.     my $file = $in->getValue(".file");
  789.     my $data = "";
  790.  
  791.     &VMware::VMServerd::errorReset();
  792.  
  793.     # Validation on the input parameters
  794.     if( !defined($file) || $file eq "" ) {
  795.     &VMware::VMServerd::errorPost("No file specified");
  796.     return 0;
  797.     }
  798.  
  799.     if (! isAbsolutePath($file)) {
  800.     &VMware::VMServerd::errorPost("Not an absolute path: $file");
  801.     return 0;
  802.     }
  803.  
  804.     # Make sure this is a regular file.  Otherwise, we could get
  805.     # some bad side effects.
  806.     if( ! -f $file ) {
  807.         &VMware::VMServerd::errorPost("File '$file' is not a regular file");
  808.     return 0;
  809.     }
  810.    
  811.     if (!unlink($file)) {
  812.       &VMware::VMServerd::errorPost("Could not remove file $file. Reason: $!");
  813.       return 0;
  814.     }
  815.  
  816.     return(1);
  817. }
  818.  
  819. #
  820. # Gets the virtual network interfaces on the system.
  821. #
  822. # Input format:
  823. #   No input args.
  824. # Output includes information about each file.  
  825. # Output format:
  826. #
  827. #   .net.interface := the name of the virtual interface
  828. #   .net.type := bridged | hostonly
  829. #   <others> depends on Host_GetBridgedNet_Handler and 
  830. #            Host_GetHostOnlyNet_Handler
  831. #
  832. sub Host_GetVirtualNet_Handler($$) {
  833.     my $in = shift;
  834.     my $out = shift; 
  835.     Host_GetBridgedNet_Handler($in, $out);
  836.     Host_GetHostOnlyNet_Handler($in, $out);
  837.     return(1);
  838. }
  839.  
  840.  
  841. #
  842. # Gets the bridged network interfaces on the system.
  843. #
  844. # Input format:
  845. #   No input args.
  846. # Output includes information about each file.  
  847. # Output format:
  848. #
  849. #   .net.interface := the name of the virtual interface
  850. #   .net.type := bridged
  851. #   .net.ethdev := the real ethernet device to which the virtual 
  852. #                  interface is bound
  853. #
  854. sub Host_GetBridgedNet_Handler($$) {
  855.     my $in = shift;
  856.     my $out = shift; 
  857.  
  858.     my @nets = $out->listElementNames("out", "^net");
  859.     my $i = $#nets + 1;
  860.     $out->setValue(".net[$i].interface", "vmnet0");
  861.     $out->setValue(".net[$i].type", "bridged");
  862.     $out->setValue(".net[$i].ethdev", "eth0");
  863.     return(1);
  864. }
  865.  
  866.  
  867. #
  868. # Gets the hostonly network interfaces on the system.
  869. #
  870. # Input format:
  871. #   No input args.
  872. # Output includes information about each file.  
  873. # Output format:
  874. #
  875. #   .net.interface := the name of the virtual interface
  876. #   .net.type := hostonly
  877. #   .net.hostaddr := the host address for the host machine
  878. #   .net.netmask := the netmask for the virtual network
  879. #
  880. sub Host_GetHostOnlyNet_Handler($$) {
  881.     my $in = shift;
  882.     my $out = shift; 
  883.  
  884.     my @nets = $out->listElementNames("out", "^net");
  885.     my $i = $#nets + 1;
  886.     $out->setValue(".net[$i].interface", "vmnet1");
  887.     $out->setValue(".net[$i].type", "hostonly");
  888. #    $out->setValue(".net[$i].hostaddr", "192.168.0.1");
  889. #    $out->setValue(".net[$i].netmask", "255.255.255.0");
  890.     return(1);
  891. }
  892.  
  893. # Convert a string to its equivalent shell representation
  894. sub shell_string {
  895.   my $single_quoted = shift;
  896.  
  897.   $single_quoted =~ s/'/'"'"'/g;
  898.   # This comment is a fix for emacs's broken syntax-highlighting code --hpreg
  899.   return '\'' . $single_quoted . '\'';
  900. }
  901.  
  902. 1;
  903.  
  904.